﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using Sagacious.DataSources;

namespace Sagacious.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class LogManager
    {
        /// <summary>
        /// 
        /// </summary>
        public LogManager()
        {
            _logs = new Dictionary<string, ILogSource>();
        }

        public event EventHandler LogChanged;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        public void AddLogFile(string filename)
        {
            LogFileWatcher _watcher = new LogFileWatcher(filename);
            _watcher.LogChanged += _watcher_LogChanged;
            _watcher.Enabled = true;
            _logs.Add(_watcher.Name, _watcher);
        }

        void _watcher_LogChanged(object sender, EventArgs e)
        {
            if (this.LogChanged != null)
            {
                this.LogChanged(this, e);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private Dictionary<string, ILogSource> _logs;
        public IReadOnlyDictionary<string, ILogSource> Logs
        {
            get
            {
                return _logs;
            }
        }

        public System.Collections.IEnumerable LogEntries
        {
            get
            {
                ILogEntryDataSource dataSource = DependencyContainer.Container.Resolve<ILogEntryDataSource>();
                return dataSource.LogEntries;
            }
        }
    }
}
